#include "err_code.h"

// 通用错误消息 (0-1)
const char* generic_messages[] = {
    "Success",
    "Null pointer detected"
};

// 窗口错误消息 (100001+)
const char* wnd_messages[] = {
    "Window cycle detected",
    "Window already has parent/child",
    "Window not found"
};

// 向量容器错误消息 (200001+)
const char* vec_messages[] = {
    "Vector out of memory",
    "Vector realloc failed",
    "Vector copy failed",
    "Null pointer or empty vector",
    "No need to resize",
    "Invalid index"
};

// 定义分类映射表
ErrorCategory error_categories[] = {
    {0, generic_messages, sizeof(generic_messages)/sizeof(generic_messages[0])},
    {100001, wnd_messages, sizeof(wnd_messages)/sizeof(wnd_messages[0])},
    {200001, vec_messages, sizeof(vec_messages)/sizeof(vec_messages[0])}
};

const char* get_error_message(MErrorCode code) {
    // 遍历所有分类
    for (size_t i = 0; i < sizeof(error_categories)/sizeof(error_categories[0]); i++) {
        ErrorCategory cat = error_categories[i];
        
        // 检查错误码是否属于当前分类范围
        if (code >= cat.base_code && code < cat.base_code + cat.msg_count) {
            int index = code - cat.base_code;
            return cat.messages[index];
        }
    }
    
    // 未找到匹配分类
    return "Unknown error code";
}